package com.vancouver.knows.ui.activity.citydiscount;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pangu.util.StringUtil;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.R;
import com.vancouver.knows.business.bean.BusinessImageInfo;
import com.vancouver.knows.business.bean.CityDiscountDetailInfo;
import com.vancouver.knows.ui.base.BaseActivity;
import com.vancouver.knows.ui.view.HackyViewPager;
import com.vancouver.knows.util.AppUtils;
import com.vancouver.knows.util.CircleTransform;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by Chris Li on 2016-08-19.
 */
public class BusinessImageViewerActivity extends BaseActivity implements PhotoViewAttacher.OnViewTapListener {

    public static final String POST_ID_EXTRA = "post_id_extra";
    public static final String INDEX_EXTRA = "index_extra";

    @Bind(R.id.image_view_pager)
    HackyViewPager mImageViewPager;
    @Bind(R.id.page_number)
    TextView mPageNumberText;

    @Bind(R.id.top_container)
    RelativeLayout topContainer;
    @Bind(R.id.bottom_container)
    RelativeLayout bottomContainer;
    @Bind(R.id.user_avatar)
    ImageView mUserAvatar;
    @Bind(R.id.user_name)
    TextView mUsername;
    @Bind(R.id.upload_date)
    TextView mUploadDate;
    @Bind(R.id.caption)
    TextView mImageCaption;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_business_image_viewer);

        String postId = getIntent().getStringExtra(POST_ID_EXTRA);
        if (StringUtil.isEmptyString(postId)) {
            finish();
        }

        int index = getIntent().getIntExtra(INDEX_EXTRA, 0);

        CityDiscountDetailInfo cityDiscountDetailInfo = imContext.getBusinessModule().getCacheManager().getDiscountDetailInfoMap().get(postId);
        if (cityDiscountDetailInfo != null) {
            final List<BusinessImageInfo> businessImageInfoList = cityDiscountDetailInfo.getBusinessUserImages();
            if (businessImageInfoList != null && businessImageInfoList.size() > 0) {
                final int pageCount = businessImageInfoList.size();
                mPageNumberText.setText(getString(R.string.page_number, 1, pageCount));
                mImageViewPager.setAdapter(new ImageViewPagerAdapter(this, businessImageInfoList));
                mImageViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        mPageNumberText.setText(getString(R.string.page_number, position + 1, pageCount));
                        setImageInfo(businessImageInfoList.get(position));
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
                mImageViewPager.setCurrentItem(index);
                setImageInfo(businessImageInfoList.get(index));
            } else {
                finish();
            }
        } else {
            finish();
        }
    }

    @OnClick(R.id.close_icon)
    public void onCloseIconClicked() {
        onBackPressed();
    }

    private void setImageInfo(BusinessImageInfo imageInfo) {
        Picasso.with(this).load(imageInfo.getUserInfo().getAvatar())
                .transform(new CircleTransform())
                .fit()
                .into(mUserAvatar);

        mUsername.setText(imageInfo.getUserInfo().getName());
        mUploadDate.setText(AppUtils.getDateStr(this, imageInfo.getDateAdded()));
        mImageCaption.setText(imageInfo.getImageCaption());
    }

    @Override
    public void onViewTap(View view, float x, float y) {
        if (topContainer.getVisibility() == View.GONE) {
            topContainer.setVisibility(View.VISIBLE);
            bottomContainer.setVisibility(View.VISIBLE);
        } else {
            topContainer.setVisibility(View.GONE);
            bottomContainer.setVisibility(View.GONE);
        }
    }

    public static class ImageViewPagerAdapter extends PagerAdapter {

        private Context mContext;
        private List<BusinessImageInfo> mImages;

        public ImageViewPagerAdapter(Context context, List<BusinessImageInfo> images) {
            this.mContext = context;
            this.mImages = images;
        }

        @Override
        public int getCount() {
            return mImages.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            BusinessImageInfo businessImageInfo = mImages.get(position);
            LayoutInflater inflater = LayoutInflater.from(mContext);
            ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.image_view_layout, container, false);
            container.addView(layout);

            PhotoView image = (PhotoView) layout.findViewById(R.id.image);

            Picasso.with(mContext).load(businessImageInfo.getImageUrl())
                    .resize(AppUtils.getWindowWidth(mContext), 0)
                    .into(image);

            image.setOnViewTapListener((PhotoViewAttacher.OnViewTapListener) mContext);

            return layout;
        }
    }
}
